package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenNeedInfoDO;
import com.ruoyi.project.men.domain.MenNeedTypeDO;
import com.ruoyi.project.men.service.MenNeedInfoService;
import com.ruoyi.project.men.service.MenNeedTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 需求详情Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menNeedInfo")
public class MenNeedInfoController extends BaseController
{
	@Autowired
	private MenNeedInfoService menNeedInfoService;
	@Autowired
	private MenNeedTypeService menNeedTypeService;

	/**
	 * 查询需求详情列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenNeedInfoDO menNeedInfo)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNeedInfo);
		stringObjectMap.put("delFlag","0");
		List<Map<String, Object>> menNeedInfoList = menNeedInfoService.menNeedList(stringObjectMap);
		return getDataTable(menNeedInfoList);
	}

	/**
	 * 导出需求详情列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:export')")
	@Log(title = "需求详情", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenNeedInfoDO menNeedInfo)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNeedInfo);
		List<MenNeedInfoDO> menNeedInfoList = menNeedInfoService.list(stringObjectMap);
		if(menNeedInfoList.size()>0){
			for (MenNeedInfoDO menNeedInfoDO : menNeedInfoList) {
				MenNeedTypeDO menNeedTypeDO = menNeedTypeService.get(menNeedInfoDO.getTypeId());
				if(menNeedTypeDO!=null){
					menNeedInfoDO.setTypeId(menNeedTypeDO.getName());
				}
			}
		}
		ExcelUtil<MenNeedInfoDO> util = new ExcelUtil<MenNeedInfoDO>(MenNeedInfoDO.class);
		return util.exportExcel(menNeedInfoList, "menNeedInfo");
	}

	/**
	 * 获取需求详情详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menNeedInfoService.get(id));
	}

	/**
	 * 新增需求详情
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:add')")
	@Log(title = "需求详情", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenNeedInfoDO menNeedInfo)
	{
		menNeedInfo.setId(IdUtil.simpleUUID());
		menNeedInfo.setDelFlag(0);
		menNeedInfo.setCaAt(new Date());
		return toAjax(menNeedInfoService.save(menNeedInfo));
	}

	/**
	 * 修改需求详情
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:edit')")
	@Log(title = "需求详情", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenNeedInfoDO menNeedInfo)
	{
		menNeedInfo.setUpAt(new Date());
		return toAjax(menNeedInfoService.update(menNeedInfo));
	}

	/**
	 * 上架
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:edit')")
	@Log(title = "需求详情", businessType = BusinessType.UPDATE)
	@PutMapping("/put/{ids}")
	public AjaxResult put(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenNeedInfoDO menNeedInfo = menNeedInfoService.get(string);
				menNeedInfo.setStype(0);
				menNeedInfoService.update(menNeedInfo);
			}
		}
		return toAjax(ids.length);
	}
	/**
	 * 下架
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:edit')")
	@Log(title = "需求详情", businessType = BusinessType.UPDATE)
	@PutMapping("/under/{ids}")
	public AjaxResult under(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenNeedInfoDO menNeedInfo = menNeedInfoService.get(string);
				menNeedInfo.setStype(1);
				menNeedInfoService.update(menNeedInfo);
			}
		}
		return toAjax(ids.length);
	}
	/**
	 * 删除需求详情
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedInfo:remove')")
	@Log(title = "需求详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenNeedInfoDO menNeedInfo = menNeedInfoService.get(string);
				menNeedInfo.setDelFlag(1);
				menNeedInfo.setUpAt(new Date());
				menNeedInfoService.update(menNeedInfo);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menNeedInfoService.deleteMenNeedInfoByIds(ids));
	}
}
